/*--
 * Echo.cs: Encapsulates either indoc or its arguments as a plaintext string.
 *--
 * For more information, please visit the Metashell Project site:
 *   http://code.google.com/p/metashell/
 *--
 * (c) 2006 Christopher E. Granade.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; exactly version 2
 * of the License applies to this software.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#region Using Statements

	using System;
	using System.Collections.Generic;
	using System.IO;
	using System.Xml;
	using Metashell.Core;
	using Metashell.Core.Utility;

#endregion

namespace Metashell.Commands.Basic {

	public class Echo : ICommand {
		
		#region Constants
		
			public const string NamespaceURI =
				MetadocNamespaces.PlainText;
		
		#endregion
		
		public void RegisterCore(Metashell.Core.Core core_) {
			// We don't need the core here, so just nod and smile.
		}
		
		public string name {
			get { return "echo"; }
		}
		
		public void Run(
			string parameters,
			Metadoc indoc,
			out Metadoc outdoc
		) {
		
			// Echo has two modes:
			//   1) Encapsulate indoc.
			//   2) Encapsulate parameters.
			// In case indoc != null, (1) takes priority.
			// In either case, we simply store the results to a string
			// and then encapsulate as the next step.
			
			string toecho;
			
			if (indoc.data != null) {
				StringWriter sw = new StringWriter();
				indoc.data.Save(sw);
				toecho = sw.ToString();
			} else {
				toecho = parameters;
			}
			
			// Now, do the actual encapsulation.
			
			outdoc = XML.EncapsulateString(toecho);			
						
		}

		
	}

}